const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      idCardFront: '',
      idCardBack: '',
	  realName: '',
	  idCard: '',
    },
    
    // 选择身份证正面
    chooseIDCardFront() {
      this.chooseImage('idCardFront');
    },
    
    // 选择身份证反面
    chooseIDCardBack() {
      this.chooseImage('idCardBack');
    },
	// 通用选择图片方法
	  chooseImage(type) {
	    wx.chooseImage({
	      count: 1,
	      sizeType: ['original', 'compressed'],
	      sourceType: ['album', 'camera'],
	      success: (res) => {
	        const tempFilePaths = res.tempFilePaths;
	        
	        // 上传图片到服务器
	        this.uploadFile(tempFilePaths[0], type);
	      }
	    });
	  },
	 // 上传文件到服务器
	  uploadFile(filePath, type) {
	    wx.showLoading({
	      title: '上传中...',
	    });
	    
	    wx.uploadFile({
	      url: app.configData.shop.api_url + '/index.php?ctl=Upload&action=uploadImage&typ=json',
	      filePath: filePath,
	      name: 'upfile',
	      header: {
	          'content-type': 'multipart/form-data'
	      },
	      formData: {
	          'user': 'test'
	      },
	      success: (res) => {
	        wx.hideLoading();
	        const data = JSON.parse(res.data);
	        if(data.state === "SUCCESS") {
	          wx.showToast({
	            title: '上传成功',
	            icon: 'success'
	          });
	          // 可以在这里保存服务器返回的文件路径
			  this.setData({
			    [type]: data.url
			  });
	        } else {
	          wx.showToast({
	            title: '上传失败',
	            icon: 'none'
	          });
	        }
	      },
	      fail: (err) => {
	        wx.hideLoading();
	        wx.showToast({
	          title: '上传失败',
	          icon: 'none'
	        });
	      }
	    });
	  },
// 输入事件处理
  handleRealName(e) {
    this.setData({
      realName: e.detail.value // 实时更新到data
    })
  },
  // 输入事件处理
   handleIdCard(e) {
     this.setData({
       idCard: e.detail.value // 实时更新到data
     })
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
	new app.ToastPannel();
	
	  var that = this
	// 加载保存的用户信息
	that.requests();
  },
requests:function(){
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Personal_Resume&met=resumeDetail&typ=json',
      method: 'GET',
      data: { k: key, u: uid },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.status == 200) {
			const user_info = res.data.data
			that.setData({
				realName: user_info.realName,
				idCard: user_info.idCard,
				idCardFront: user_info.idCardFront,
				idCardBack: user_info.idCardBack
			})
        } else {
          that.show('数据加载失败')
        }
  
      }
    })
  },
  saveInfo() {
  	const that = this
  	const key = wx.getStorageSync('key')
  	const uid = wx.getStorageSync('u')
  	const id = that.data.editIndex
  	if(that.data.realName == ''){
  		app.show('请输入真实姓名')
  	} else if(that.data.idCard == '') {
  		app.show('请输入身份证号')
  	} else if(that.data.idCardFront == '') {
  		app.show('请上传身份证正面照片')
  	} else if(that.data.idCardBack == '') {
  		app.show('请上传身份证反面照片')
  	} else {
  		const parme = {
  			k: key, 
  			u: uid,
  			id: id,
			realName: that.data.realName,
			idCard: that.data.idCard,
			idCardFront: that.data.idCardFront,
			idCardBack: that.data.idCardBack
  		};
  		wx.request({
  			url: app.configData.shop.api_url + '/index.php?ctl=Personal_Resume&met=addResumeInfo&typ=json',
  			data: parme,
  			method: 'POST', 
  			header: {
  				"Content-Type": "application/x-www-form-urlencoded"
  			}, // 设置请求的 header
  			success: function (res) {
  				if(res.data.status == 200){
  					app.show('保存成功')
  					wx.redirectTo({
  						url: '../personal_resume/personal_resume',
  					})
  				}else{
  					app.show('保存失败')
  				}
  			},
  			fail: function (res) {
  			  
  			}
  		})
  	}
  }
})